package com.cloudm.technician.ui.modules.cloudm_box.install_check

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.extensions.openActivity
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BasePermissionsFragment
import com.cloudm.technician.ui.modules.cloudm_box.change_info.ChangeInfoActivity
import com.cloudm.technician.ui.modules.scan_qr_code.ScanQrCodeActivity
import com.cloudm.technician.ui.widget.OnceClickListener
import kotlinx.android.synthetic.main.fragment_install.*

/**
 * 作者: wesley
 * 时间: 2017/12/25
 * 描述: ui
 */
class InstallCheckFragment : BasePermissionsFragment<InstallCheckPresenter>(),
        InstallCheckContract.View {

    private var info: DeviceInfo? = null

    override val resource: Int
        get() = R.layout.fragment_install

    companion object {
        private val REQUEST_CODE_PERMISSION_FOR_CAMERA = 1
        fun newInstance(info: DeviceInfo?) = InstallCheckFragment().apply {
            arguments = Bundle().apply {
                putParcelable("info", info)
            }
        }
    }

    override fun initInject() {
        DaggerInstallCheckComponent.builder().appComponent(
                appComponent).installCheckPresenterModule(
                InstallCheckPresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        info = bundle.getParcelable("info")
    }

    override fun initVariables() {

    }

    override fun initViews(view: View) {

    }

    override fun initListener() {
        btnInstallCheck.setOnClickListener(object : OnceClickListener() {
            override fun onOnceClick(view: View) {
                requestPermissionsForScanQrCode()
            }
        })
    }

    override fun scanRrCodeGrantedSuccess() {
        super.scanRrCodeGrantedSuccess()
        val intent = Intent(mContext, ScanQrCodeActivity::class.java).apply {
            putExtras(Bundle().apply {
                putInt("flag", 2)
            })
        }
        startActivityForResult(intent, REQUEST_CODE_PERMISSION_FOR_CAMERA)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        when (requestCode) {
            REQUEST_CODE_PERMISSION_FOR_CAMERA -> {
                presenter?.dealWithScanResult(data?.extras?.getString("result"), info)
            }
        }
    }

    override fun showSnEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.sn_code_cannot_empty))
    }

    override fun showSnFormatErrMsg() {
        mContext.toast(mContext.resources.getString(R.string.sn_format_error))
    }

    override fun showDeviceInfoEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.device_info_can_not_empty))
    }

    override fun scanSuccess(info: DeviceInfo, newSn: String) {
        val intent = Intent(mContext, ChangeInfoActivity::class.java).apply {
            putExtras(Bundle().apply {
                putParcelable("info", info)
                putString("sn", newSn)
            })
        }
        mContext.openActivity(intent)
    }
}